import http from '@/utils/http'
/**
 * 用来获取负责人
 */
export const getPerson = () => {
  return async (dispatch) => {
    const res = await http.get('/sys/user/simple')
    dispatch({
      type: 'editDepart/getPerson',
      payload: res.data.data,
    })
  }
}

/**
 * 新增部门
 * @param data 表单数据
 * @returns {(function(): Promise<*>)|*}
 */
export const addDepart = (data) => {
  return async () => {
    const res = await http.post('/company/department', data)
    if (!res.data.success) {
      throw new Error(res.data.message)
    }
  }
}

/**
 * 部门详情，查、改、删
 * @param id
 * @param status add、update、del
 * @returns {(function(): Promise<*>)|*}
 */
export const handlePart = (id, status, data) => {
  return async () => {
    const url = `/company/department/${id}`
    console.log(url)
    switch (status) {
      case 'del':
        http.delete(url)
        break
      case 'update':
        http.put(url, data)
        break
      case 'get':
        const res = await http.get(url)
        return res.data
    }
  }
}
